﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;


namespace MVC.Web.UI.UIMenu
{
    public static class NavMenuHelper
    {
        public static string NavMenu(this AjaxHelper helper, dynamic menuDataSource)
        {
            if (menuDataSource == null)
                return null;

            var mDataSource = ((TopNavigationItemCollection)menuDataSource);

            StringBuilder sb = new StringBuilder();
            sb.Append("<div class=\"tTopNav\"><div class=\"tSectionsNavWrap\"><ul class=\"tSectionsNavList\">");

            int TopNavigationCount = 0;
            foreach (var item in mDataSource)
            {                
                sb.Append("<li style=\"width: 103px\" id=\"_TopNavigation1_id" + TopNavigationCount + "\" class=\"mItem\">");

                if (!string.IsNullOrEmpty(item.ActionName))
                    sb.Append(ActionLink(helper, item.Text, item.ActionName, item.ControllerName, (AjaxOptions)item.AjaxOptions, new { @class = "tp" }));
                else
                    sb.Append(string.Format("<a class=\"tp\">{0}</a>",item.Text));

                sb.Append("<span class=\"mArrow\"></span>");
                sb.Append("<div style=\"left: 65px\" class=\"mSubMenu\"><div class=\"mSubMenuInner mSubMenuInnerTP\">");

                int columnCount = 1;
                foreach (var column in item.Columns)
                {
                    if (columnCount > 3)
                        throw new NotSupportedException("You can have max 3 Columns (for now) :)");

                    sb.Append(string.Format("<div class=\"col{0}\">",columnCount));

                    sb.Append(string.Format("<strong>{0}</strong>",column.Title));

                    sb.Append("<ul class=\"mSubMenuMajorList\">");
                   
                    foreach (var cItem in column.Items)
                    {
                        sb.Append("<li><span class=\"mTxt\">");

                        if (!string.IsNullOrEmpty(cItem.ActionName))
                            sb.Append(ActionLink(helper, cItem.Text, cItem.ActionName, cItem.ControllerName, (AjaxOptions)cItem.AjaxOptions, null));
                        else
                            sb.Append(cItem.Text);

                        sb.Append(string.Format("<em>{0}</em>", cItem.Description));
                        sb.Append("</span></li>");
                    }

                    sb.Append("</ul>");
                    sb.Append("</div>");

                    columnCount++;
                }

                sb.Append("</div></div>");

                TopNavigationCount++;
            }
            sb.Append("</ul></div></div>");

            //sb.AppendLine(GetNavMenuScript());

            MvcHtmlString sbMvc = MvcHtmlString.Create(sb.ToString());
            return sbMvc.ToHtmlString();
        }

        private static string ActionLink(AjaxHelper helper, string linkText, string actionName, string controllerName, AjaxOptions options, dynamic htmlAttributes)
        {
            return System.Web.Mvc.Ajax.AjaxExtensions.ActionLink(helper, linkText, actionName, controllerName, null, options, htmlAttributes).ToHtmlString();
        }

        public static string GetNavMenuScript()
        {
            return
                "<script type=\"text/javascrip\">" +
                "function onSuccess() {" +
                    "$(\".mSubMenu\").each(function () {" +
                        "$(this).attr('class', 'mSubMenu');" +
                    "});" +
                "}" +

                "$(\"ul.tSectionsNavList li.mItem\").hover(" +
                                  "function () {" +
                                      "$(this).addClass(\"hover\");" +
                                  "}," +
                                  "function () {" +

                                      "$(this).removeClass(\"hover\");" +
                                  "}" +
                                ");" +

                "$(\".mItem span.mArrow\").click(function () {" +
                    "if ($(this).next().attr('class') == 'mSubMenu') {" +
                        "$(\".mSubMenuDisplayed\").attr('class', 'mSubMenu');" +
                        "$(\".mItemSelected\").attr('class', 'mItem');" +
                        "$(\".mArrowSelected\").attr('class', 'mArrow');" +

                        "$(this).next().attr('class', 'mSubMenu mSubMenuDisplayed');" +
                        "$(this).attr('class', 'mArrow mArrowSelected');" +
                        "$(this).parent().attr('class', 'mItem mItemSelected');" +
                    "} else {" +
                        "$(this).next().attr('class', 'mSubMenu');" +
                        "$(this).attr('class', 'mArrow');" +
                        "$(this).parent().attr('class', 'mItem');" +
                    "}" +
                "}); " +
            "</script>";
        }

        public static T Cast<T>(object obj, T type)
        {
            return (T)obj;
        }
    }
}
